package top.drinyeit.nongmao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.common.XEditText;
import top.drinyeit.nongmao.presenter.WithdrawPresenter;

/**
 * Created by wu on 2018/1/6.
 */

public class WithdrawActivity extends BaseActivity {

    private TextView withdraw, number, name;
    private XEditText money;
    private LinearLayout zfb, bank;
    private ImageView default_btn0, default_btn1;
    private TextView withdrawDetail;
    String balance;
    double balanceNumber;
    private WithdrawPresenter withdrawPresenter;

    String type = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_withdraw);

        withdrawDetail = findViewById(R.id.tv_withdraw_detail);
        money = findViewById(R.id.money);
        withdraw = findViewById(R.id.withdraw);
        number = findViewById(R.id.number);
        name = findViewById(R.id.name);
        zfb = findViewById(R.id.zfb);
        bank = findViewById(R.id.bank);

        default_btn0 = findViewById(R.id.default_btn0);
        default_btn1 = findViewById(R.id.default_btn1);

        initIntent();
        initData();
        init();

        setTitle(true,"提现");
    }

    public void initIntent() {
        Bundle bundle = getIntent().getExtras();
        balanceNumber = bundle.getDouble("balance");
        money.setHint("可提现总额:" + balanceNumber);
    }

    public void initData() {
        withdrawPresenter = new WithdrawPresenter(this);
    }

    @Override
    public void init() {
        //提现明细
        withdrawDetail.setVisibility(View.GONE);
        withdrawDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WithdrawActivity.this,WithdrawDetailActivity.class);
                startActivity(intent);
            }
        });
        //限制2位小数
//        money.addTextChangedListener(new TextWatcher() {
//            double doubleBalance = Double.valueOf(balanceNumber);
//            public void afterTextChanged(Editable edt) {
//                String temp = edt.toString();
//                int posDot = temp.indexOf(".");
//                if (posDot <= 0) return;
//                if (temp.length() - posDot - 1 > 2) {
//                    edt.delete(posDot + 3, posDot + 4);
//                }
//
//                if (edt != null && !edt.equals("")) {
//                    if (doubleBalance != -1) {
//                        double markVal = 0;
//                        try {
//                            markVal = Double.parseDouble(edt.toString());
//                        } catch (NumberFormatException e) {
//                            markVal = 0;
//                        }
//                        if (markVal > doubleBalance) {
//                            money.setText(String.valueOf(doubleBalance));
//                            money.setSelection(String.valueOf(doubleBalance).length());
//                        }
//                        return;
//                    }
//                }
//            }

//            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//            }
//
//            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//                if (arg1 > 1) {
//                    if ( doubleBalance != -1) {
//                        double num = Double.parseDouble(arg0.toString());
//                        if (num > doubleBalance) {
//                            arg0 = String.valueOf(doubleBalance);
//                            money.setText(arg0);
//                        }
////                        } else if (num < MIN_MARK) {
////                            s = String.valueOf(MIN_MARK);
////                            edit.setText(s);
////                        }
//                        money.setSelection(arg0.length());
//                    }
//                }
//            }
//        });
        money.setMaxmumFilter(balanceNumber,2);

        zfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                default_btn0.setSelected(true);
                default_btn1.setSelected(false);
                setType();
            }
        });

        bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                default_btn1.setSelected(true);
                default_btn0.setSelected(false);
                setType();
            }
        });

        withdraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (number.getText().toString().length() == 0) {
                    showToastMsg("提现帐号不能为控");
                    return;
                }
                if (name.getText().toString().length() == 0) {
                    showToastMsg("姓名不能为空");
                }
                if (type.equals("0")) {
                    showToastMsg("请选择提现类型");
                }
                if (money.getText().length() == 0) {
                    showToastMsg("请输入提现金额");
                }
                withdrawPresenter.withdraw(number.getText().toString(), name.getText().toString(), type, money.getText().toString());
            }
        });
    }

    private void setType() {
        if (default_btn0.isSelected()) {
            type = "1";
        } else if (default_btn1.isSelected()) {
            type = "3";
        }
    }
}
